package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    BrandService brandService;


    @GetMapping
    public List<Brand> findAll(){
        return brandService.list();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.removeById(id);
        return new Result(true,20000,"删除成功");
    }

    @PostMapping
    public Result<Brand> add(@RequestBody Brand brand){
        brandService.save(brand);
        return new Result(true,20000,"增加ok");
    }

 /*   @PostMapping("search/{page}/{size}")
    public Result<Brand> fingPage(@PathVariable("page")Integer page,
                                  @PathVariable("size")Integer size,
                                  @RequestBody Object searchMap){
       HashMap<String,String> map = ( HashMap<String,String> ) searchMap;
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery();

        wrapper.like(Brand::getName,map.get("name"))
                .eq(Brand::getId,map.get("id"));
        Page<Brand> brandPage = new Page<>(page, size);
//          (                                       )
        List<Brand> list = brandPage.getRecords();
        return new Result(true,20000,"查询ok");
    }*/

    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id")Integer id){
         brandService.getById(id);
         return new Result(true,20000,"查询ok");
    }

    @PutMapping("/{id}")
    public Result<Brand> update(@PathVariable("id")Integer id,
                                @RequestBody Brand brand){
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery();
        wrapper
                .eq(Brand::getId,brand.getId());
//                .eq(Brand::getImage,brand.getImage());
//                .eq(Brand::getLetter,brand.getLetter())
//                .eq(Brand::getSeq,brand.getSeq())
//                .eq(Brand::getName,brand.getName());
        brandService.update(brand,wrapper);
        return new Result(true,20000,"更新ok");
    }

    @GetMapping("search/{page}/{size}")
    public Result<Brand> findPage(@PathVariable("page")Integer page,
                                  @PathVariable("size")Integer size){
        Page<Brand> page1 = new Page<>(page, size);
//        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery();
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.ge("id","2");
        Brand brand = brandService.getById(wrapper);
        return new Result(true,20000,"查询ok");
    }

   /* @GetMapping("search/{page}/{size}")
    public Result<Brand> findPage(@PathVariable("page")Integer current,
                                @PathVariable("size")Integer size,
                                @RequestBody Brand brand){
        LambdaQueryWrapper<Brand> wrapper = Wrappers.<Brand>lambdaQuery();

        wrapper.ge(Brand::getId,brand.getId());

        Page<Brand> page = new Page<>(current,size);
        return new Result(true,20000,"查询ok");
    }*/
}
